#include <bits/stdc++.h>
using namespace std;
long long a[1000005];
int x,n;
long long m[1000005];

int main()
{
	cin >> x >> n;
  	for (int i = 0; i < n; i++)	cin >> m[i];
  	
	
	int k = 0;
	for (int i = 0; i < 50; i++)
	{
		a[i] = pow(x,i);
      	if (a[i] > 1e9)
        {
          k = i;
          break;
        }
	}
	
	for (int i = 0; i < n; i++)
	{
		
		for (int j = k; j >= 0; j--)
		{
          if (a[j] <= m[i])
          {
            cout << m[i] - a[j] << endl;
            break;
          }
        }
	}
	return 0;
}






#include <bits/stdc++.h>
using namespace std;
long long a[1000005];
int x,n;
long long m[1000005];

int main()
{
	cin >> x >> n;
  	for (int i = 0; i < n; i++)	cin >> m[i];
  	
	
	int p = 0;
	for (p = 0; p < 50; p++)
	{
		a[p] = pow(x,p);
      	if (a[p] > 1e9)  break;
	}
	
	for (int i = 0; i < n; i++)
	{
		
		for (int j = p; j >= 0; j--)
		{
          if (a[j] <= m[i])
          {
            cout << m[i] - a[j] << endl;
            break;
          }
        }
	}
	return 0;
}

